package com.atguigu.boot.controller;

import com.atguigu.boot.bean.Person;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:朱思博
 * @date:2021/5/5-{15:06}   测试普通参数和基本注解
 *
 *
 *
 * <li>@PathVariable（路径变量）</li>
 * <li>@RequestHeader（获取请求头）</li>
 * <li>@RequestParam（获取请求参数）</li>
 * <li>@CookieValue（获取cookie值）</li>
 * <li>@RequestBody（获取请求体[POST]）</li>
 *
 * <li>@RequestAttribute（获取request域属性）</li>
 * <li>@MatrixVariable（矩阵变量）</li>
 */
@RestController
public class ParametersAnnotationController {
    /**
     * 数据绑定：页面提交的请求数据（GET、POST）都可以和对象属性进行绑定
     * @param person
     * @return
     */
    @PostMapping("/saveuser")
    public Person saveuser(Person person){

        return person;
    }


    //  RestFul风格:car/2/owner/zhangsan
    @GetMapping("/car/{id}/owner/{username}")   // @RequestMapping(method = RequestMethod.GET)
    public Map<String,Object> getCar(
            @PathVariable("id") Integer id,    // @PathVariable("id"): 指定参数名,获取路径上的请求变量
            @PathVariable("username") String name,
            @PathVariable Map<String,String> pv,    // 也可以不明确指定
            @RequestHeader("User-Agent") String userAgent,  // @RequestHeader获取指定的请求头内容
            @RequestHeader Map<String,String> header,    // 这样的固定写法可以获取所有的请求头
            @RequestParam("age") Integer age,   // 该注解获取指定请求参数，即?&
            @RequestParam("inters") List<String> inters,
            @RequestParam Map<String,String> params    // 获取全部请求参数
//            @CookieValue("Idea") String Idea, // 获取指定Cookie的值
//            @CookieValue("_ga") Cookie cookie
    ){
        Map<String,Object> map = new HashMap<>();

        map.put("id",id);
        map.put("name",name);
        map.put("pv",pv);
        map.put("userAgent",userAgent);
        map.put("headers",header);
        map.put("age",age);
        map.put("inters",inters);
        map.put("params",params);
//        map.put("Idea",Idea);
//        System.out.println(cookie.getName()+"===>"+cookie.getValue());
        return map;
        /*
            @PathVariable:
                url：
                    http://localhost:8080/car/3/owner/lisi

                代码：
                    @PathVariable("id") Integer id,
                    @PathVariable("username") String name,
                    @PathVariable Map<String,String> pv

                结果：
                    {
                        "pv":{
                            "id":"3",
                            "username":"lisi"
                         },
                        "name":"lisi",
                        "id":3
                    }


            @RequestHeader:
                url：
                    http://localhost:8080/car/3/owner/lisi

                代码：
                    @PathVariable("id") Integer id,
                    @PathVariable("username") String name,
                    @PathVariable Map<String,String> pv,
                    @RequestHeader("User-Agent") String userAgent,
                    @RequestHeader Map<String,String> header

                结果：
                {
                    "headers": {
                            "host": "localhost:8080",
                            "connection": "keep-alive",
                            "cache-control": "max-age=0",
                            "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Microsoft Edge\";v=\"90\"",
                            "sec-ch-ua-mobile": "?0",
                            "upgrade-insecure-requests": "1",
                            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.51",
                            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,**;q=0.8,application/signed-exchange;v=b3;q=0.9",
                            "sec-fetch-site": "same-origin",
                            "sec-fetch-mode": "navigate",
                            "sec-fetch-user": "?1",
                            "sec-fetch-dest": "document",
                            "referer": "http://localhost:8080/",
                            "accept-encoding": "gzip, deflate, br",
                            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
                            "cookie": "Idea-ef6f1ea=aa2de4c0-0aa0-4573-9e9b-495ec08b2681"
                    },
                    "pv": {
                        "id": "3",
                        "username": "lisi"
                    },
                    "name": "lisi",
                    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.51",
                    "id": 3
                 }

            @RequestParam:
                url:
                    http://localhost:8080/car/3/owner/lisi?age=18&inters=game&inters=swim

                代码：
                    @RequestParam("age") Integer age,   // 该注解获取请求参数，即?&
                    @RequestParam("inters") List<String> inters,
                    @RequestParam Map<String,String> params

                结果：
                    {
                        "inters": [
                            "game",
                            "swim"
                         ],
                         "age": "18",
                         "params": {
                            "age": "18",
                            "inters": "game"    // 因为接受所有请求参数的参数类型是一个Map<String,String>，所以只接受了集合中第一个值
                          },
                    }

         */
    }


    /*
        @RequestBody:       必须是Post请求，即适用于表单提交
            获取请求体：
     */
    @PostMapping("/save")
    public Map postMethod(@RequestBody String content){
        Map<String,Object> map = new HashMap<>();
        map.put("content",content);
        // "content": "userName=sisiu&email=2508458180%40qq.com"
        return map;
    }


    // 矩阵变量
    //1、语法： 请求路径：/cars/sell;low=34;brand=byd,audi,yd
    //2、SpringBoot默认是禁用了矩阵变量的功能
    //      手动开启：原理。对于路径的处理。UrlPathHelper进行解析。
    //              removeSemicolonContent（移除分号内容）支持矩阵变量的
    //3、矩阵变量必须有url路径变量才能被解析
    @GetMapping("/cars/{path}")
    public Map carsSell(@MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") List<String> brand,
                        @PathVariable("path") String path){
        Map<String,Object> map = new HashMap<>();

        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }

    // /boss/1;age=20/2;age=10

    @GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
                    @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
        Map<String,Object> map = new HashMap<>();

        map.put("bossAge",bossAge);
        map.put("empAge",empAge);
        return map;

    }
}
